<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>选择时间</title>
        <meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no,minimal-ui">
        <link rel="stylesheet" href="../../../style/mobile/mainO.css"/>
        <link rel="stylesheet" href="../../../style/mobile/operationAnalysis.css"/>
        <link rel="stylesheet" href="../../../style/mobile/mui.min.css"/>
        <link rel="stylesheet" href="../../../style/mobile/mui.picker.min.css"/>
        
        <style>
        	.header-icon{
				background-size: 30%;
			}
        </style>
        
    </head>
    
	<body style="background-color: #ffffff;">
		<header class="header-bar header-bar-nav">
    		<a class="header-icon header-left" href="javascript:back();"></a>
    		<h1 class="header-title">选择时间</h1>
    	</header>
    	
    	<div class="dateMain f10">
    		<div class="dateSelect">
				<span class="main-left">开始时间</span>    	
				<span class="main-right btn" id='resultStarts' data-options='{"type":"date"}'>请选择开始时间</span> 
    		</div>
    		<div class="dateSelect">
				<span class="main-left">结束时间</span>    	
				<span class="main-right btn" id='resultEnd' data-options='{"type":"date"}'>请选择结束时间</span> 
    		</div>
    		
    		<div class="dateMainBtn">
    			<a href="javascript:void(0);" id="backToAnalysis">确定</a>
    		</div>
    	</div>
    		
        <script src="../../../script/jquery-1.10.2.min.js"></script>
        <script src="../../../script/mui.min.js"></script>
        <script src="../../../script/mui.picker.min.js"></script>
        <script src="../../../script/comUr.js"></script>
        
    	<script type="text/javascript">
	    	//当前时间,时间往前推一年
			var thisYear,beforeYear;
	    	var sessionId = getcache("sessionId"); //获取缓存中的sessionId
		    var myDate = new Date();
			myDate.getFullYear(); //获取完整的年份(4位,1970-????)
			
			thisYear = myDate.getFullYear(); //今年年份
			beforeYear = myDate.getFullYear() - 1;  //去年年份
			
			var myDate = new Date(); //今天的完整日期 年-月-日
			var theDate = myDate.getFullYear()+"-"+((myDate.getMonth()+1)<10?"0":"")+(myDate.getMonth()+1)+"-"+(myDate.getDate()<10?"0":"")+myDate.getDate();
			
    		//选择时间层弹出
    		var startsDate = '';
    		var endDate = '';
			(function($) {
				$.init();
				var resultStarts = $('#resultStarts')[0];
				var resultEnd = $('#resultEnd')[0];
				
				var btnStarts = $('#resultStarts');
				var btnEnd = $('#resultEnd');
				
				//开始时间
				btnStarts.each(function(i, btn) {
					btn.addEventListener('tap', function() {
						/*var optionsJson = this.getAttribute('data-options') || '{}';
						var options = JSON.parse(optionsJson);
						var id = this.getAttribute('id');*/
						var options = {type : 'date', beginYear : ""+ beforeYear +"", endYear : ""+ thisYear +""};
						var picker = new $.DtPicker(options);
						picker.show(function(rs) {
							startsDate = resultStarts.innerText = rs.text;
							picker.dispose();
							//日期初步选择验证
							if(startsDate > theDate ){
								mui.alert('请选择今天或今天之前的日期');
								return false;
							}
						});
					}, false);
				});
				
				//结束时间
				btnEnd.each(function(i, btn) {
					btn.addEventListener('tap', function() {
						/*var optionsJson = this.getAttribute('data-options') || '{}';
						var options = JSON.parse(optionsJson);
						var id = this.getAttribute('id');*/
						var options = {type : 'date', beginYear : ""+ beforeYear +"", endYear : ""+ thisYear +""};
						var picker = new $.DtPicker(options);
						picker.show(function(rs) {
							endDate = resultEnd.innerText = rs.text;
							picker.dispose();
							//日期初步选择验证
							if(endDate > theDate){
								mui.alert('请选择今天或今天之前的日期');
								return false;
							}
						});
					}, false);
				});
			})(mui);
			
			//计算两个日期之间的天数
			function dateValidation(){
				var a = new Date(''+ startsDate +'').getTime();
				var b= new Date(''+ endDate +'').getTime();
				var c = 60*60*24*1000;
				var res = (b - a)/c;
				console.log(parseInt(res));
				if(parseInt(res) > 90 ){
					mui.alert('选择日期区间不能超过90天');
					return false;
				}else if(parseInt(res) < 0){
					mui.alert('结束日期不能早于开始日期');
					return false;
				}
				return true;
			}
			//日期选择验证
			$("#backToAnalysis").click(function(){
				if(!startsDate){
					mui.alert('请选择开始日期');
				}else if(!endDate){
					mui.alert('请选择结束日期');
				}else{
					if(!dateValidation()){
						return false;
					}
					window.location.href = "operationAnalysis.html?startsDate=" + startsDate + "&endDate=" + endDate +"&sessionId=" + sessionId;
					return true;
				}
			});
			
			function back(){
				window.location.href = "operationAnalysis.html?sessionId=" + sessionId;
			}
			
    	</script>
    	
	</body>
</html>
